package com.v1.web.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.v1.cache.CategoryCache;
import com.v1.lo.BaseLO;
import com.v1.lo.Statement;
import com.v1.model.Category;
import com.v1.model.Comment;
import com.v1.model.Item;
import com.v1.model.Tag;

public class CategoryAction extends DefaultAction {

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Integer categoryid = getInteger(request, "categoryid", 0);
		CategoryCache categoryCache = (CategoryCache) getBean("CategoryCache");
		if (categoryid == 0 || !categoryCache.contains(categoryid)) {
			return INDEX;
		}

		List<Category> list = categoryCache.getSubCategorys(categoryid);
		Category category = categoryCache.get(categoryid);
		setAttribute(request, "category", category);

		ActionForward forward = null;
		Item item = null;
		if (CollectionUtils.isNotEmpty(list)) {
			setAttribute(request, "items", list);
			forward = mapping.findForward("category");
		} else {
			forward = item(mapping, request, categoryid);
			item = (Item) getAttribute(request, "item");
			recommends(category, item, request);
			tags(category, item, request);
		}
		if (item != null) {
			String jsp = item.getJsp();
			if (StringUtils.isNotEmpty(jsp)) {
				return new ActionForward("", jsp, false, "");
			}
		}

		String jsp = category.getJsp();
		if (StringUtils.isNotEmpty(jsp)) {
			return new ActionForward("", jsp, false, "");
		}

		return forward;
	}

	private void tags(Category category, Item item, HttpServletRequest request) {
		BaseLO lo = (BaseLO) getBean("BaseLO");
		Tag tag = new Tag();
		tag.setCategoryid(category.getCategoryid());
		if (item != null) {
			tag.setItemid(item.getItemid());
		}
		List<Tag> tags = lo.queryForList(tag, Statement.Tag_getTags);
		setAttribute(request, "tags", tags);
	}

	protected void recommends(Category category, Item item, HttpServletRequest request) {
		int categoryid = category.getCategoryid();
		if (item != null) {
			BaseLO lo = (BaseLO) getBean("BaseLO");
			List<Item> sameTags = lo.queryForList(item, Statement.Item_getSameTagItemByItem);
			List<Item> filteredSameTags = new ArrayList<Item>();
			for (Item tag : sameTags) {
				Integer tagCategoryid = tag.getCategoryid();
				if (categoryid == tagCategoryid) {
					continue;
				}
				filteredSameTags.add(tag);

				if (filteredSameTags.size() > 8) {
					setAttribute(request, "sametags", filteredSameTags);
					return;
				}
			}
			setAttribute(request, "sametags", filteredSameTags);
		}

		Integer parentid = category.getParentid();
		CategoryCache categoryCache = (CategoryCache) getBean("CategoryCache");
		List<Category> subCategorys = categoryCache.getSubCategorys(parentid);
		List<Category> recommends = new ArrayList<Category>(subCategorys);
		for (Iterator<Category> it = recommends.iterator(); it.hasNext();) {
			if (((Category) it.next()).getCategoryid() == categoryid) {
				it.remove();
				break;
			}
		}

		if (recommends.size() < 8) {
			Category parent = categoryCache.get(parentid);
			List<Category> elderCategorys = categoryCache.getSubCategorys(parent.getParentid());
			recommends.addAll(elderCategorys);
		}
		setAttribute(request, "recommends", recommends);
	}

	private ActionForward item(ActionMapping mapping, HttpServletRequest request, int categoryid) {
		Item cond = new Item();
		cond.setCategoryid(categoryid);
		BaseLO lo = (BaseLO) getBean("BaseLO");
		Statement statement = Statement.Item_getThumbs;
		CategoryCache categoryCache = (CategoryCache) getBean("CategoryCache");
		if (categoryCache.getRoot(categoryid).getCategoryid() == 3) {
			// hezuoshe
			statement = Statement.Item_getItem;
		}
		List<Item> items = lo.queryForList(cond, statement);
		if (CollectionUtils.isEmpty(items)) {
			return ERROR;
		}
		setAttribute(request, "items", items);

		Integer itemid = getInteger(request, "itemid");
		if (itemid == null) {
			itemid = items.get(0).getItemid();
		}

		cond.setItemid(itemid);
		Item item = lo.queryForObject(cond, Statement.Item_getItem);
		setAttribute(request, "item", item);

		comment(request, itemid);

		return mapping.findForward("item");
	}

	private void comment(HttpServletRequest request, int itemid) {
		Comment comment = new Comment();
		comment.setItemid(itemid);
		BaseLO lo = (BaseLO) getBean("BaseLO");
		int rowCount = lo.queryForCount(comment, Statement.Comment_getCommentCount);
		if (rowCount != 0) {
			Integer pageNo = getInteger(request, "page", 1);
			comment.init(rowCount, pageNo);
			List<Comment> comments = lo.queryForList(comment, Statement.Comment_getComment);
			setAttribute(request, "comments", comments);
			setAttribute(request, "pageinfo", comment);
		}
	}

}